package com.chunxiadong.controller;

import com.chunxiadong.entity.Blog;
import com.chunxiadong.entity.RecommendBlog;
import com.chunxiadong.entity.User;
import com.chunxiadong.pojo.BlogInfo;
import com.chunxiadong.service.AdminService;
import com.chunxiadong.service.BlogInfoService;
import com.chunxiadong.service.BlogService;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @auther QiuShangcheng
 * @create 2021/6/9
 */
@Controller
public class StartController {
    private final Logger log = Logger.getLogger(this.getClass());

    @Autowired
    AdminService adminService;

    @Autowired
    BlogInfoService blogInfoService;

    @Autowired
    BlogService blogService;


    /**
     * 博客首页获取信息
     * @param session
     * @return
     */
    @RequestMapping("/")
    public String  start(HttpSession session, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, Model model){
        List<RecommendBlog> recommendBlog = blogService.getRecommendBlog(new RecommendBlog(null,null,null,true));
        PageInfo<Blog> pageInfo = blogService.getAllBlogByPage(pageNum, 5);
        BlogInfo blogInfo = blogInfoService.getBlogInfo();
        User blogger = adminService.getAdmin("chunxiadong");
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("recommendBlog", recommendBlog);
        session.setAttribute("blogInfo",blogInfo);
        session.setAttribute("blogger",blogger);
        return "index";
    }
}
